package com.hunan.api.catv.action.news;

import com.hunan.api.catv.action.BaseAction;
import com.hunan.api.catv.annotation.MemberAuth;
import com.hunan.api.catv.common.Api;
import com.hunan.api.catv.entity.Result;
import com.hunan.api.catv.entity.vo.NewsVo;
import com.hunan.api.catv.service.biz.cache.CacheBiz;
import com.hunan.api.catv.service.biz.news.NewsBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

/**
 * 资讯接口
 */
@RestController
@RequestMapping(Api.news)
@Validated
public class NewsAction extends BaseAction {

    @Autowired
    NewsBiz newsBiz;

    @Autowired
    CacheBiz cacheBiz;

    @GetMapping(value = {"/list"})
    public Result list(Integer columnId, int pageNum, HttpServletRequest request) {
        // cacheBiz.newsList(columnId, pageNum, getPageSize(request))
        return buildOK(newsBiz.newsList(columnId, pageNum, getPageSize(request)), "");
//        return buildOK(cacheBiz.newsList(columnId, pageNum, getPageSize(request)),"");
    }

    @MemberAuth
    @GetMapping(value = {"/special/{code}"})
    public Result specialNews(HttpServletRequest request,String code) {
        return buildOK(newsBiz.specialNews(getMemberId(request),code), "");
    }

    @GetMapping(value = {"/info"})
    public Result info(Integer newsId) {
        if (ObjectUtils.isEmpty(newsId)) {
            return buildRequire("newsId require");
        }
        return buildOK(cacheBiz.newsInfo(newsId), "");
    }

    @GetMapping(value = {"/infoV2"})
    public Result infoV2(Integer newsId) {
        if (ObjectUtils.isEmpty(newsId)) {
            return buildRequire("newsId require");
        }
        return buildOK(cacheBiz.newsInfoV2(newsId), "");
    }

    @GetMapping(value = {"/play"})
    public Result play(Integer newsId) {
        return buildResult(newsBiz.play(newsId));
    }

    /**
     * 新版本短视频详情
     */
    @GetMapping(value = "/getNewsInfo/{newsId}")
    public Result getNewsInfo(@PathVariable("newsId") Integer newsId, HttpServletRequest request) {
        return buildOK(newsBiz.getNewsInfos(newsId, getMemberIdV2(request)), "");
    }

    /**
     * 新版本短视频列表
     */
    @GetMapping(value = "/getNewsList/{columnId}/{pageNum}")
    public Result getNewsList(@PathVariable("columnId") Integer columnId, @PathVariable("pageNum") int pageNum, HttpServletRequest request) {
        return buildOK(newsBiz.getNewsList(columnId, pageNum, getPageSize(request), getMemberIdV2(request)), "");
    }

    /**
     * 根据位置标签获取短视频列表
     */
    @PostMapping(value = "/getNewsListByPosition")
    public Result getNewsListByPositio(int pageNum, int pageSize, String positio, HttpServletRequest request) {
        return buildOK(newsBiz.getNewsListByPosition(pageNum, pageSize, positio, getMemberIdV2(request)), "");
    }

    /**
     * 获取个人发布短视频列表
     */

    @GetMapping("/getNewsListByMemberId/{pageNum}/{pageSize}/{id}")
    public Result getNewsListByMemberId(HttpServletRequest request, @PathVariable("id") Integer id, @PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize) {
        return buildOK(newsBiz.getNewsListByMemberId(pageNum, pageSize, getMemberIdV2(request), id), "");
    }


    /**
     * 获取个人发布短视频  不需要token
     */
    @GetMapping("/getNewsListById/{pageNum}/{pageSize}/{id}")
    public Result getNewsListById(@PathVariable("id") Integer id, @PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize) {
        return buildOK(newsBiz.getNewsListByMemberId(pageNum, pageSize, null, id), "");
    }

    /**
     * 获取短视频置顶列表
     */
    @GetMapping(value = {"/getNewsTopList/{column}"})
    public Result getNewsTopList(@NotNull @PathVariable("column") Integer column, HttpServletRequest request) {
        return buildOK(newsBiz.getNewsTopList(column, getMemberIdV2(request)), "");
    }

    /**
     * 保存用戶發佈視頻
     */
    @MemberAuth
    @PostMapping("/saveUserReleaseVideos")
    public Result saveUserReleaseVideos(@RequestBody NewsVo newsVo, HttpServletRequest request) {
        if (StringUtils.isEmpty(newsVo.getTitle())) {
            return buildRequire("title require");
        }
        return buildOK(newsBiz.saveUserReleaseVideos(newsVo, getMemberId(request)), "");
    }


}
